﻿namespace AnyOffice.Components
{
    using log4net;
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Collections;
    using System.Data;
    using System.Data.SqlClient;
    using System.Web;
    using System.Web.Caching;
    using System.Reflection;

    public class UserCache
    {
        private static ILog log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        private string userId;

        public UserCache()
        {
            this.userId = HttpContext.Current.User.Identity.Name;
            if (HttpContext.Current.Cache["AnyOffice_User_" + this.userId] == null) this.Insert();
        }

        public UserCache(string userid)
        {
            this.userId = userid;
            if (HttpContext.Current.Cache["AnyOffice_User_" + this.userId] == null) this.Insert();
        }

        private void Insert()
        {
            try
            {
                string str = "SELECT [Users].[UserName],[Users].[DeptId],[Department].[DeptName],[Users].[PrivId],[User_Priv].[PrivName],[Users].[PostPriv],[Users].[PostDept],[User_Priv].[FuncIdStr],[Users].[NickName],[Users].[Avatar],[Users].[MenuType],[Users].[SmsOn],[Users].[CallSound],[Users].[Theme],[Users].[State],[Users].[EmailCapacity],[Users].[FolderCapacity],[Users].[MenuHide],[Users].[UserId2],[Users].[MytableLeft],[Users].[MytableRight] FROM [dbo].[Users],[dbo].[Department],[dbo].[User_Priv] WHERE [Users].[UserId] = '" + this.userId + "' AND [Users].[DeptId] = [Department].[DeptId] AND [Users].[PrivId] = [User_Priv].[PrivId]";
                Hashtable table = new Hashtable();
                SqlDataReader reader = SqlHelper.ExecuteReader(ConfigManager.ConnectionString, CommandType.Text, str);
                if (reader.Read())
                {
                    table.Add("UserName", reader.GetString(0));
                    table.Add("DeptId", reader.GetInt32(1));
                    table.Add("DeptName", reader.GetString(2));
                    table.Add("PrivId", reader.GetInt32(3));
                    table.Add("PrivName", reader.GetString(4));
                    table.Add("PostPriv", !reader.IsDBNull(5) ? reader.GetString(5) : "0");
                    table.Add("PostDept", !reader.IsDBNull(6) ? reader.GetString(6) : string.Empty);
                    table.Add("FuncIdStr", !reader.IsDBNull(7) ? reader.GetString(7) : string.Empty);
                    table.Add("NickName", !reader.IsDBNull(8) ? reader.GetString(8) : string.Empty);
                    table.Add("Avatar", reader.GetInt32(9));
                    table.Add("MenuType", reader.GetBoolean(10));
                    table.Add("SmsOn", reader.GetBoolean(11));
                    table.Add("CallSound", reader.GetInt32(12));
                    table.Add("Theme", reader.GetInt32(13));
                    table.Add("State", reader.GetInt32(14));
                    table.Add("EmailCapacity", reader.GetInt32(15));
                    table.Add("FolderCapacity", reader.GetInt32(0x10));
                    table.Add("MenuHide", reader.GetBoolean(0x11));
                    table.Add("UserId2", !reader.IsDBNull(0x12) ? reader.GetString(0x12) : string.Empty);
                    table.Add("MytableLeft", !reader.IsDBNull(0x13) ? reader.GetString(0x13) : string.Empty);
                    table.Add("MytableRight", !reader.IsDBNull(20) ? reader.GetString(20) : string.Empty);
                }
                reader.Close();
                table = Hashtable.Synchronized(table);
                HttpContext.Current.Cache.Insert("AnyOffice_User_" + this.userId, table, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(60.0));
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled) { log.Error("", exception); }
            }
        }

        public static void Update(string O0lO1l)
        {
            try
            {
                string str = "SELECT [Users].[UserName],[Users].[DeptId],[Department].[DeptName],[Users].[PrivId],[User_Priv].[PrivName],[Users].[PostPriv],[Users].[PostDept],[User_Priv].[FuncIdStr],[Users].[NickName],[Users].[Avatar],[Users].[MenuType],[Users].[SmsOn],[Users].[CallSound],[Users].[Theme],[Users].[State],[Users].[EmailCapacity],[Users].[FolderCapacity],[Users].[MenuHide],[Users].[UserId2],[Users].[MytableLeft],[Users].[MytableRight] FROM [dbo].[Users],[dbo].[Department],[dbo].[User_Priv] WHERE [Users].[UserId] = '" + O0lO1l + "' AND [Users].[DeptId] = [Department].[DeptId] AND [Users].[PrivId] = [User_Priv].[PrivId]";
                Hashtable table = new Hashtable();
                SqlDataReader reader = SqlHelper.ExecuteReader(ConfigManager.ConnectionString, CommandType.Text, str);
                if (reader.Read())
                {
                    table.Add("UserName", reader.GetString(0));
                    table.Add("DeptId", reader.GetInt32(1));
                    table.Add("DeptName", reader.GetString(2));
                    table.Add("PrivId", reader.GetInt32(3));
                    table.Add("PrivName", reader.GetString(4));
                    table.Add("PostPriv", !reader.IsDBNull(5) ? reader.GetString(5) : "0");
                    table.Add("PostDept", !reader.IsDBNull(6) ? reader.GetString(6) : string.Empty);
                    table.Add("FuncIdStr", !reader.IsDBNull(7) ? reader.GetString(7) : string.Empty);
                    table.Add("NickName", !reader.IsDBNull(8) ? reader.GetString(8) : string.Empty);
                    table.Add("Avatar", reader.GetInt32(9));
                    table.Add("MenuType", reader.GetBoolean(10));
                    table.Add("SmsOn", reader.GetBoolean(11));
                    table.Add("CallSound", reader.GetInt32(12));
                    table.Add("Theme", reader.GetInt32(13));
                    table.Add("State", reader.GetInt32(14));
                    table.Add("EmailCapacity", reader.GetInt32(15));
                    table.Add("FolderCapacity", reader.GetInt32(0x10));
                    table.Add("MenuHide", reader.GetBoolean(0x11));
                    table.Add("UserId2", !reader.IsDBNull(0x12) ? reader.GetString(0x12) : string.Empty);
                    table.Add("MytableLeft", !reader.IsDBNull(0x13) ? reader.GetString(0x13) : string.Empty);
                    table.Add("MytableRight", !reader.IsDBNull(20) ? reader.GetString(20) : string.Empty);
                }
                reader.Close();
                table = Hashtable.Synchronized(table);
                HttpContext.Current.Cache.Insert("AnyOffice_User_" + O0lO1l, table, null, Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(60.0));
            }
            catch (Exception exception)
            {
                if (log.IsErrorEnabled) { log.Error("", exception); }
            }
        }

        public int Avatar
        {
            get
            {
                return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["Avatar"].ToString());
            }
        }

        public int CallSound
        {
            get
            {
                return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["CallSound"].ToString());
            }
        }

        public int DeptId
        {
            get
            {
                return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["DeptId"].ToString());
            }
        }

        public string DeptName
        {
            get
            {
                return ((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["DeptName"].ToString();
            }
        }

        public int EmailCapacity
        {
            get
            {
                return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["EmailCapacity"].ToString());
            }
        }

        public int FolderCapacity
        {
            get
            {
                return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["FolderCapacity"].ToString());
            }
        }

        public string FuncIdStr
        {
            get
            {
                return ((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["FuncIdStr"].ToString();
            }
        }

        public bool MenuHide
        {
            get
            {
                return Convert.ToBoolean(((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["MenuHide"].ToString());
            }
        }

        public bool MenuType
        {
            get
            {
                return Convert.ToBoolean(((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["MenuType"].ToString());
            }
        }

        public string MytableLeft
        {
            get
            {
                return ((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["MytableLeft"].ToString();
            }
        }

        public string MytableRight
        {
            get
            {
                return ((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["MytableRight"].ToString();
            }
        }

        public string NickName
        {
            get
            {
                return ((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["NickName"].ToString();
            }
        }

        public string PostDept
        {
            get
            {
                return ((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["PostDept"].ToString();
            }
        }

        public string PostPriv
        {
            get
            {
                return ((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["PostPriv"].ToString();
            }
        }

        public int PrivId
        {
            get
            {
                return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["PrivId"].ToString());
            }
        }

        public string PrivName
        {
            get
            {
                return ((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["PrivName"].ToString();
            }
        }

        public bool SmsOn
        {
            get
            {
                return Convert.ToBoolean(((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["SmsOn"].ToString());
            }
        }

        public int State
        {
            get
            {
                return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["State"].ToString());
            }
        }

        public int Theme
        {
            get
            {
                return Convert.ToInt32(((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["Theme"].ToString());
            }
        }

        public string UserId
        {
            get
            {
                return this.userId;
            }
            set
            {
                this.userId = value;
            }
        }

        public string UserId2
        {
            get
            {
                return ((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["UserId2"].ToString();
            }
        }

        public string UserName
        {
            get
            {
                return ((Hashtable) HttpContext.Current.Cache["AnyOffice_User_" + this.userId])["UserName"].ToString();
            }
        }
    }
}

